package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.service.AddressBookService;
import com.itheima.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;

import java.util.List;
//地址管理
@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    //新增
    @PostMapping
    public R<AddressBook> save(@RequestBody AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookService.save(addressBook);
        return R.success(addressBook);
    }

    //查询指定用户的全部地址
    @GetMapping("/list")
    public R<List<AddressBook>> list(AddressBook addressBook) {
        //这个已经在新增的时候设置过了但是笔记里有,所有添加不添加无所谓
        addressBook.setUserId(BaseContext.getCurrentId());
        //条件构造器
        //这个是查询AddressBook表中的某个数据
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        //这个是要获取AddressBook表中不为空的id拿过来,用数据库里AddressBOOK::getUserId
        //和自己写的addressBook.getUserId对比
        queryWrapper.eq(null != addressBook.getUserId(),
                AddressBook::getUserId, addressBook.getUserId());
        //按着指定排序,desc是降序
        queryWrapper.orderByDesc(AddressBook::getUpdateTime);
        //SQL:select * from address_book where user_id = ? order by update_time desc
        return R.success(addressBookService.list(queryWrapper));
    }

    //只能有一个默认地址,设置默认地址
    @PutMapping("default")
    public R<AddressBook> setDefault(@RequestBody AddressBook addressBook) {
        LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
        //这个是没有条件的所有事直接对比
        wrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        //设置默认为0
        wrapper.set(AddressBook::getIsDefault, 0);

        //SQL:update address_book set is_default = 0 where user_id =?
        addressBookService.update(wrapper);
        //若果点了默认辞职那么他的默认0会变成1
        addressBook.setIsDefault(1);
        //SQL:update address_book set is_default =1 where id =?
        //by根据id来修改
        addressBookService.updateById(addressBook);
        return R.success(addressBook);

    }

    //根据id查询地址
    @GetMapping("/{id}")
    //根据id查ID是long类型的
    //因为传过来的是一串数字的id所以用@PathVaridble这个注解
    public R get(@PathVariable Long id) {
        //用addressBookService中的获取id方法来获取id---getbyid根据id获取
        //然后赋值
        AddressBook addressBook = addressBookService.getById(id);
        //判断这个获取的地址不要为空
        if (addressBook != null) {
            //如果不为空那么就是找到了就返回被id赋值的addressBook
            return R.success(addressBook);
        } else {
            //如果没有找到和这个id赋值的addressBook那么就用错误方法返回错误语句
            return R.error("没有找到该对象");
        }
    }

    //查询默认地址
    @GetMapping("default")
    public R<AddressBook> getDefault(){
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        queryWrapper.eq(AddressBook::getIsDefault, 1);
        //SQL:select*from address_book where user_id = ? and is_default = 1
        AddressBook addressBook = addressBookService.getOne(queryWrapper);
        if (null == addressBook){
            return R.error("没有找到对象");
        }else {
            return  R.success(addressBook);
        }
    }
}
